<template>
	<view>
		<!-- <view class="page"> -->
		<view class="top" :style="`padding-top:`+$store.state.StatusBar.statusBar + `px`">
			<view class="top-icon" @click="back">
				<u-icon name="arrow-left" size="42rpx" color='#fff'></u-icon>
			</view>
			<!-- <view class="top-title c-fff">{{title}}</view> -->
		</view>
		<view class="body" :style="`margin-top:`+$store.state.StatusBar.statusBar + `px`">
			<view class="list">
				<view class=" flex items-cneter flex-between px-24 mb-30" @click="to('/pages/m-pages/m-settle-account/index')">
					<view class="label c-fff">账户安全</view>
					<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
				</view>
				<u-line></u-line>
				<view class="flex items-center flex-between px-24 mb-30 mt-30" @tap="clearCacheConfirm">
					<view class="label c-fff">内存清理</view>
					<view class="flex items-center">
						<view style="position: absolute; right:40rpx;" class="c-fff mr-24">{{cacheSize}}</view>
						<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
					</view>
				</view>
				<u-line></u-line>
				<view class="flex items-center flex-between px-24 mb-30 mt-30" @tap="goAgreement">
					<view class="label c-fff">用户协议</view>
					<view class="flex items-center">
						<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
					</view>
				</view>
				<u-line></u-line>
				<view class="flex items-center flex-between px-24 mb-30 mt-30" @tap="goPrivacy">
					<view class="label c-fff">隐私政策</view>
					<view class="flex items-center">
						<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
					</view>
				</view>
				<u-line></u-line>
				<view class="flex items-center flex-between px-24 mb-30 mt-30">
					<view class="label c-fff">个性化推送</view>
					<view class="flex items-center">
						<switch :checked="checked" @change="pushChange"/>
					</view>
				</view>
				<u-line></u-line>
				<view class="flex items-center flex-between px-24 mb-30 mt-30">
					<view class="label c-fff">版本号</view>
					<view class="flex items-center">
						<view style="position: absolute; right:40rpx;" class="c-fff mr-24">V{{version}}</view>
						<!-- <u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon> -->
					</view>
				</view>
			</view>
			<view class="button" @click="quit">退出登录</view>
		</view>
	</view>
	</view>
</template>

<script>
	import {
		to
	} from '@/utils/index.js';
	export default {
		data() {
			return {
				info: {},
				cacheSize: '',
				idNum: '',
				version: '',
				realName: '',
				checked: false
			}
		},
		onLoad(option) {
		},
		onShow(option) {
			this.getInfo();
			if(uni.getStorageSync("push")){
				this.checked = uni.getStorageSync("push");
			}
		},
		methods: {
			to,
			back() {
				to()
			},
			getAppInfo() {
				// #ifdef APP-PLUS
				let self = this;
				plus.cache.calculate(size => {
					if (size < 1024) {
						self.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.cacheSize = Math.floor((size / 1024) * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						self.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
					}
				});
				// #endif
				// #ifdef H5
				let storageInfo = uni.getStorageInfoSync()
				console.log(storageInfo)
				this.cacheSize = storageInfo.currentSize.toString() + "KB";
				// #endif
			},
			clearCacheConfirm() {
				uni.showModal({
					title: "操作确认",
					content: "确认要清理缓存吗?",
					success: (res) => {
						if (res.confirm) {
							this.clearCache()
						}
					}
				})
			},
			//清除缓存
			clearCache() {
				let that = this;
				// #ifdef H5
				uni.clearStorageSync()
				uni.showToast({
					title: '缓存清理完成',
					icon: 'none',
				});
				setTimeout(() => {
					uni.redirectTo({
						url: '/im/login/login'
					});
				}, 1000)
				// #endif

				// #ifdef APP-PLUS
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
									that.getAppInfo(); // 重新计算缓存
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});

				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.getAppInfo();
					});
				}
				// #endif
			},
			quit() {
				let vipManage = uni.getStorageSync('vip-manage');
				console.log("set-vipManage: " + JSON.stringify(vipManage));
				uni.$TUIKit.logout().then(() => {
					uni.showToast({
						title: '退出成功',
						icon: 'none',
					});
					
				}).catch((err) => {
					uni.showToast({
						title: '退出成功',
						icon: 'none',
					});
					setTimeout(() => {
						uni.redirectTo({
							url: '/im/login/login',
						});
					}, 1000);
				});
				uni.removeStorageSync('userInfo');
			},
			async getInfo() {
				// // 获取系统信息
				// const systemInfo = uni.getSystemInfoSync();
				// // 应用程序版本号
				// const version = systemInfo.appVersion;
				// this.version = version
				
				if(plus){
					plus.runtime.getProperty(plus.runtime.appid, (info) => {
						this.version = info.version;
					});
				}
				
				
				this.getAppInfo()
				// let res = await this.$c.post("/app/exchange/getList")
				// if(res.code != 0){
				// 	uni.navigateTo({
				// 	  url: '/im/login/login'
				// 	});
				// }
				// this.info = res.data
			},
			async submit() {
				let res = await this.$c.post("/app/auth/realNameAuthIos?idNum=" + this.idNum + "&realName=" + this
					.realName)
				if (res.code != 0) {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					return;
				}
				uni.showToast({
					title: "提交成功",
					icon: "none"
				})
				setTimeout(() => {
					// this.getInfo()
					this.$router.go(0)
				}, 1000)
			},
			goAgreement() {
				getApp().globalData.webviewUrl='https://www.xksndf2002.cn/agreement-lalaq.html'
				uni.navigateTo({
					url: '/pages/webview/webview'
				})
			},
			goPrivacy() {
				getApp().globalData.webviewUrl='https://www.xksndf2002.cn/privacy-lalaq.html'
				uni.navigateTo({
					url: '/pages/webview/webview'
				})
			},
			pushChange(e){
				uni.setStorageSync("push", e.detail.value);
				console.log("个性化："+JSON.stringify(e.detail.value));
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		width: 100vw;
		padding: 30rpx;
		height: 100vh;
		// background: linear-gradient(#e3ecfb, #fdedf0, #fff, #fff);
	}

	.top {
		position: fixed;
		top: 0rpx;
		width: 100%;
		// background-color: #e3ecfb;
		z-index: 999;
		text-align: center;
		vertical-align: middle;
		padding: 10rpx;
	}

	.top-icon {
		position: absolute;
		left: 30rpx;
		z-index: 99;
		width: 30rpx;
		height: 30rpx;
	}

	.top-title {
		line-height: 50rpx;
		display: block;
		height: 50rpx;
		z-index: 99;
	}

	.body {
		padding-top: 20rpx;
		text-align: center;

		.title {
			margin-top: 40rpx;
			font-size: 24rpx;
		}

		.top-infr-icon {
			width: 26rpx;
			height: 26rpx;
			position: absolute;
			right: 0rpx;
		}

		.button {
			background: linear-gradient(180deg, #FDB175 5%, #F49C66 100%);
			color: #fff;
			display: inline-block;
			padding: 30rpx 0rpx;
			transform: translateX(-30rpx);
			border-radius: 50rpx;
			font-size: 32rpx;
			width: 80%;
			margin-top: 80rpx;
		}

		.mark {
			width: 100%;
			margin-top: 30rpx;
			text-align: left;
			font-size: 20rpx;
			color: #9d9395;
		}

		.list {
			margin-top: 60rpx;

			.item {
				// width:90vw;
				width: 100%;
				padding: 0 24rpx;
				height: 80rpx;
				line-height: 80rpx;
				margin: 30rpx 24rpx;
				box-sizing: border-box;
				transform: translateX(-30rpx);
				position: relative;

				.label {
					position: absolute;
					left: 40rpx;
				}

				border-bottom: 1px solid #e2e2e2;
			}
		}
	}
</style>